package com.catazy.ocrdemo.ui.activity.scan.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.catazy.mvvmlib.ext.view.click
import com.catazy.ocrdemo.R
import com.catazy.ocrdemo.databinding.ItemScanTypeBinding
import com.chad.library.adapter4.BaseQuickAdapter

/**
 * 作者: Tao
 * 日期: 2024/3/15
 * 时间: 10:54
 * 描述:
 */
class ScanTypeAdapter:BaseQuickAdapter<String,ScanTypeAdapter.VH>() {

    private var selectPosition = 6

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: String?) {
        holder.viewBinding.itemType.text = item

        if (selectPosition == position){
            holder.viewBinding.itemType.setTextColor(context.resources.getColor(R.color.blue))
        } else {
            holder.viewBinding.itemType.setTextColor(context.resources.getColor(R.color.gray))
        }

        holder.viewBinding.itemType.click {
            val currentPosition = holder.getBindingAdapterPosition()
            if (selectPosition != currentPosition){
                notifyItemChanged(selectPosition)
                selectPosition = currentPosition
                notifyItemChanged(currentPosition)
            }
        }
    }

    class VH(
        parent: ViewGroup,
        val viewBinding: ItemScanTypeBinding = ItemScanTypeBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    ) : RecyclerView.ViewHolder(viewBinding.root)
}